package com.nextgen.services;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.nextgen.bean.Role;
import com.nextgen.mongo.core.MongoInstance;
  
/**  
 * The Class RoleService.  
 */  
@Service  
public class RoleService {  
  
     private static final Logger logger_c = Logger.getLogger(RoleService.class);  
  
     /**  
      * Get the role by id.  
      *  
      * @param roleId_p  
      *      the role id_p  
      * @return the role by id  
      */  
	public Role getRoleById(String roleId_p) {
		MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(Criteria.where("roleId").is(roleId_p));
		
		// find the saved user again.
		Role role = mongoOperation.findOne(searchUserQuery, Role.class);
		logger_c.debug("Role details with id : " + roleId_p + " is : "  + role);
		return role;
	}
	
	/**  
     * Get the role by idValue.  
     *  
     * @param roleId_p  
     *      the role id_p  
     * @return the role by id  
     */  
	public List<Role> getRoleByUserName(String userName) {
		MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(Criteria.where("userName").is(userName));
		
		// find the saved user again.
		List<Role> roleList = mongoOperation.find(searchUserQuery, Role.class);
		logger_c.debug("Role details with roleIdValue : " + userName + " is : "  + roleList);
		return roleList;
	}
  
     /**  
      * Gets all role.  
      *  
      * @return the all role  
      */  
     public List<Role> getAllRole() {
    	 MongoOperations mongoOperation = MongoInstance.getInstance();
    	 List<Role> role = mongoOperation.findAll(Role.class);
    	 
    	 return role;  
     }  
  
     /**  
      * Creates the role.  
      *  
      * @param role_p  
      *      the role_p  
      * @return the role  
      */  
     public Role createRole(Role role) {  
  
          logger_c.debug("Persisting role in database: " + role.toString());  
          MongoOperations mongoOperation = MongoInstance.getInstance();
          mongoOperation.save(role);  
  
          return role;  
     }  
  
     /**  
      * Update role.  
      *  
      * @param role_p  
      *      the role_p  
      * @return the role  
      */  
     public Role updateRole(Role role_p) {  
  
          logger_c.debug("Updating role in database: " + role_p.toString());  
          role_p.setLastUpdated(new Date());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("roleId").is(role_p.getRoleId()));
          mongoOperation.updateFirst(searchUserQuery, 
                  Update.update("password", "new password"),Role.class);
  
          return role_p;  
     }  
     
     /**  
      * Delete role.  
      *  
      * @param roleId_p  
      *      the role id_p  
      */  
     public void deleteRole(String roleId_p) {  
          logger_c.debug("Deleting role from database: " + roleId_p.toString());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("roleId").is(roleId_p));
          mongoOperation.remove(searchUserQuery, Role.class);
     }  
}